import axios from "axios";
import qs from "qs";
import { ElMessage } from 'element-plus'
import router from '@/router';

// axios.defaults.baseURL = 'http://127.0.0.1:8005/api'  //测试
axios.defaults.baseURL = 'https://www.wesavetech.com/api' //正式

//设置超时
axios.defaults.timeout = 50000;
// 1、请求拦截
axios.interceptors.request.use(
    config => {
        let token = window.sessionStorage.getItem('token');
        config.headers.Authorization = "Bearer " + token;
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);
// 2、响应拦截
axios.interceptors.response.use(
    res => {
        return res;
    },
    error => {
        if (error && error.response) {
            const status = error.response.status;
            switch (status) {
                case 400:
                    router.push('/login');
                    ElMessage.error("请求错误");
                    break;
                case 401:
                    router.push('/login');
                    ElMessage.error("未授权，请重新登录");
                    break;
                case 403:
                    router.push('/login');
                    ElMessage.error("拒绝访问");
                    break;
                case 404:
                    router.push('/login');
                    ElMessage.error("请求错误，未找到相应的资源");
                    break;
                case 408:
                    router.push('/login');
                    ElMessage.error("请求超时");
                    break;
                case 500:
                    router.push('/login');
                    ElMessage.error("服务器内部错误");
                    break;
                case 501:
                    router.push('/login');
                    ElMessage.error("网络未实现");
                    break;
                case 502:
                    router.push('/login');
                    ElMessage.error("网络错误");
                    break;
                case 503:
                    router.push('/login');
                    ElMessage.error("服务不可用");
                    break;
                case 504:
                    router.push('/login');
                    ElMessage.error("网络超时");
                    break;
                case 505:
                    router.push('/login');
                    ElMessage.error("HTTP版本不支持该请求");
                    break;
                default:
                    router.push('/login');
                    ElMessage.error("请求失败");
                    break;
            }
        } else {
            if (JSON.stringify(error).includes("timeout")) {
                ElMessage.error("服务器响应超时，请刷新页面")
            } else {
                ElMessage.error("连接服务器失败")
            }
        }
        router.push('/login');
        return Promise.reject(error);
    }
);


export default {
    /**
     * @param {String} url 
     * @param {Object} data 
     * @returns Promise
     */
    post(url, data) {
        return new Promise((resolve, reject) => {
            axios({
                method: 'post',
                url,
                data: qs.stringify(data),
            })
                .then(res => {
                    resolve(res.data)
                })
                .catch(err => {
                    reject(err)
                });
        })
    },

    get(url, data) {
        return new Promise((resolve, reject) => {
            axios({
                method: 'get',
                url,
                params: data,
            })
                .then(res => {
                    resolve(res.data)
                })
                .catch(err => {
                    reject(err)
                })
        })
    }
};
